<?php
/**
 * Created Dec. 7, 2009
 * @author : Christopher Sheldahl
 */
 
/**
 * QthParams : parameters needed to write a .qth file
 */
 class QthParams {
 		var $workingDirectory;  //string
 		var $transmitterFileName; //if file is "foo.qth"", 
 									//$transmitterFileName == "foo" - also used
 									//for radiation pattern files, dcf color 
 									//definition files and lrp
 									//files
 		var $transmitterHeight; // height in feet (a number)
 		var $lat; //latitude string 
 		var $lng; //longitude string		
 		
 		function __construct($workingDirectory, $transmitterFileName, $transmitterHeight, $lat, $lng)	{
 			
 			$this->workingDirectory = $workingDirectory;
 			$this->transmitterFileName = $transmitterFileName;
 			$this->transmitterHeight = $transmitterHeight;
 			$this->lat = $lat;
 			$this->lng = $lng;
 		}		
 } 
 
 class WriteQthUtil {
 		/** 
 		 * $params : a QthParams 
 		 * Writes a .qth file with name $params->transmitterFileName . ".qth"
 		 * to directory $params->workingDirectory
 		 * $params->transmitterHeight should be a string corresponding to a 
 		 * nonnegative integer (the number is a height in feet).  
 		 * $params->lat should be a string corresponding to either 
 		 * a decimal form of the latitude : "40.80222" 
 		 * or a form of the latitude in deg/min/sec: "40 48 8.0" for 40 deg, 
 		 * 48 min, 8.0 seconds (separate with a space).  
 		 * For the latitude : positive degree values are North, negative 
 		 * degree values are South.
 		 * $params->lng should be a string corresponding to either
 		 * a decimal form of the longitude : "74.246389" 
 		 * or a form of the longitude in deg/min/sec : "74 14 47.0" for 74 deg,
 		 * 14 min, 47.0 seconds (separate with a space).
 		 * For the longitude : positive degree values are West, negative degree
 		 * values are East.
 		 */
 		function writeFile( $params ) {
 			$workingDirectory = $params->workingDirectory;
 			$errorOccurred = false;
 			if ( ! is_readable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory . 
				" is not readable. ";
				$errorOccurred = true;
			}
			if ( ! is_writable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory . 
					" is not writable. ";
				$errorOccurred = true;
			}
			/* Commented by Tanner 12/8 ############################################
			if ( ! is_executable($workingDirectory) )
			{
				$exceptionMessage = $exceptionMessage . $workingDirectory .
					" is not executable. ";
				$errorOccurred = true;
			}
			*/
	
			if ( $errorOccurred )
			{
				throw new Exception( $exceptionMessage );
			}
 			chdir($params->workingDirectory);
 			//check antenna height
 			//$heightFloat = floatVal($params->transmitterHeight);
 			if ( $params->transmitterHeight < 0.0 )
 			{
 				$exceptionMessage = "Transmitter height must be nonnegative.";
 				throw new Exception( $exceptionMessage );
 			}
 			$fileName = $params->transmitterFileName . ".qth";
 			$file = fopen($fileName,"w") or die("Can't open file.");
 			fwrite($file, $fileName . "\n");
 			fwrite($file, $params->lat . "\n");
 			fwrite($file, $params->lng . "\n");
 			fwrite($file, $params->transmitterHeight . "\n\n\n");
 			fclose($file);
 		} //close writeFile function
 			
 }  //close class WriteQthUtil
 //MAIN
 /*
 echo("Testing...");
 $test = new QthParams();
 $test->workingDirectory = "/Users/sheldahl/splat-1.3.0";
 $test->transmitterFileName = "test";
 $test->transmitterHeight = 30.0;
 $test->lat = "40.80222";
 $test->lng = "74.246389";
 WriteQthUtil::writeFile( $test );
 echo("\nDone.\n");
 */
 ?>
 
 